package cn.tedu.egaddress0904.controller;

import cn.tedu.egaddress0904.mapper.AddressMapper;
import cn.tedu.egaddress0904.pojo.dto.AddressAddParam;
import cn.tedu.egaddress0904.pojo.entity.Address;
import cn.tedu.egaddress0904.pojo.vo.AddressInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("v1/address/")
public class AddressController {
    @Autowired
    AddressMapper addressMapper;
    @PostMapping("add")
    public String add(@RequestBody AddressAddParam addressAddParam) {
        System.out.println("添加地址："+addressAddParam);
        Address address = new Address();
        BeanUtils.copyProperties(addressAddParam,address);
        System.out.println("添加地址："+address);
        int rows = addressMapper.insertAddress(address);
        if(rows>0) {
            return "添加成功";
        }else {
            return "添加失败";
        }
    }
    @GetMapping("list")
    public List<AddressInfoVO> list() {
        List<AddressInfoVO> list = addressMapper.listAddressInfo();
        System.out.println("查询结果："+ list);
        return list;
    }
    @PostMapping("del{addressId}")
    public String delete(@PathVariable("addressId") Integer addressId) {
        int rows = addressMapper.deleteById(addressId);
        if(rows>0) {
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

}
